namespace JoinBox.Forms;

using System.IO;
using System.Windows.Forms;

public class Clipboard2
{
    // e大写的
    /// <summary>
    /// 获取剪贴板文件路径
    /// </summary>
    /// <returns></returns>
    public string? GetClipboardFilePath()
    {
        var dataObject = Clipboard.GetDataObject();
        // 得到可用格式的列表
        var formats = dataObject.GetFormats();
        for (int i = 0; i < formats.Length; i++)
        {
            var format = formats[i];

            // In case of AutoCAD 2013 this would be "AutoCAD.R19"
            if (!format.StartsWith("AutoCAD."))
                continue;
            using var stream = dataObject.GetData(format) as MemoryStream;
            if (stream == null)
                return null;
            // Unicode encoding is 16 bit (2 bytes) just like ACHAR
            using var reader = new StreamReader(stream, System.Text.Encoding.Unicode);
            if (reader == null)
                return null;

            // 0..259 = 260
            char[] text = new char[260];
            reader.Read(text, 0, 260);
            // 我们清理有大量的0字符数组中的值
            return new string(text).TrimEnd(new char[] { '0' });
        }
        return null;
    }
}